<template>
  <!-- 我的频道制作 -->
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        type="danger"
        @click="isEdit = !isEdit"
        plain
        round
        size="mini"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>

    <van-grid :gutter="10" class="my-grid">
      <van-grid-item
        class="grid-item"
        v-for="(channels, index) in myChannels"
        :key="index"
        @click="onMyChannelClick(channels, index)"
      >
        <span slot="text" class="text" :class="{ active: index === active }">{{
          channels.name
        }}</span>
        <!-- channels 数组的方法 包含的话返回 true  -->
        <!-- 显示隐藏 小叉叉 -->
        <van-icon
          v-show="isEdit && !fiexChannels.includes(channels.id)"
          slot="icon"
          name="clear"
        ></van-icon>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>

    <van-grid :gutter="10" class="recommend-grid">
      <!-- 遍历返回的数组 recommendChannels -->
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        icon="plus"
        @click="addChannels(channel)"
      />
    </van-grid>
    <!-- / 频道推荐 -->
  </div>
</template>

<script>
import {
  getAllChannels,
  addUserChannel,
  deleteUserChannel,
} from "@/api/channel.js";
import { mapState } from "vuex";
import { setItem } from "@/utils/storage";

export default {
  props: {
    myChannels: {
      type: Array,
      require: true,
    },
    active: {
      type: Number,
      require: true,
    },
  },
  data() {
    return {
      allChannels: [],
      isEdit: false,
      fiexChannels: [0],
    };
  },
  // 频道推荐
  computed: {
    recommendChannels() {
      // 定义一个储存的数组
      const channels = [];
      // 遍历所有的频道
      this.allChannels.forEach((channel) => {
        // 遍历我的频道里面的 id 跟 所有频道的 id 对比 如果一样就说明有
        const set = this.myChannels.find(
          (myChannels) => myChannels.id === channel.id
        );
        // 如果没有就 将它推送给 准备好的数组
        if (!set) {
          channels.push(channel);
        }
      });
      // 返回这个数组
      return channels;
    },
    ...mapState(["user"]),
  },

  // 获取所有的频道
  created() {
    this.loadAllChannels();
  },
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllChannels();
        console.log(data);
        this.allChannels = data.data.channels;
      } catch (err) {
        this.$toast("获取失败", err);
      }
    },

    // 添加频道
    async addChannels(channel) {
      // this.myChannels.push(channel);
      this.$emit("addChannels", channel);

      // 数据持久化
      if (this.user) {
        // 登录
        try {
          await addUserChannel({
            id: channel.id,
            seq: this.myChannels.length,
          });
        } catch (err) {
          this.$toast("添加失败");
        }
      } else {
        // 未登录
        setItem("TOUTIAO_CHANNELS", this.myChannels);
      }
    },

    async deleteChannel(channels) {
      try {
        if (this.user) {
          await deleteUserChannel(channels.id);
        } else {
          setItem("TOUTIAO_CHANNELS", this.myChannels);
        }
      } catch (err) {
        this.$toast("操作失败");
      }
    },

    // 删除频道
    onMyChannelClick(channels, index) {
      if (this.isEdit) {
        // isEdit 判断是处于 完成状态 还是 编辑状态
        // 这里为 true 所有在这里 进行删除操作
        if (this.fiexChannels.includes(channels.id)) {
          return;
        }
        this.myChannels.splice(index, 1);
        // 因为点击切换高亮是在 "updata-active" 所以 直接在这个方法里面做逻辑判断 让 active - 1
        // 如果 删除的索引 < 点击的项
        if (index <= this.active) {
          this.$emit("updata-active", this.active - 1, true);
        }
        // this.$emit("updata-active", index);
        this.deleteChannel(channels);
      } else {
        // 这里为 false 跳转操作
        this.$emit("updata-active", index, false);
      }
    },
  },
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;
  .title-text {
    font-size: 32px;
    color: #333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: #f85959;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        top: -10px;
        right: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
